function data = amrunloop(loopnum,outerindices)
%AMSTEPCHAN Summary of this function goes here
%   Detailed explanation goes here

%%%return could be the line of data, which a 2D graph uses?
%%%include outervars vec that lets transfn's of inner loop access current
%%%value?

global amrack;
global amscan;
global amaux;

thisloop=amscan.loops(loopnum);
thesechans=thisloop.allchans;
points=thisloop.points;

%or does transform go here?
%reset some plotting data or something?

thisloop.data=nan(length(thesechans),thisloop.npoints);

if loopnum>1 %initialize 2D data
    innerpoints=amscan.loops(loopnum-1).npoints;
    thisloop.data2D=nan(length([amscan.loops(loopnum-1).setchans,amscan.loops(loopnum-1).getchans]), thisloop.npoints, innerpoints);
end

%% run loops
if loopnum==1 && all([amscan.loops(loopnum).setchans.channel.isramp])
    %% buffered scan
    if amscan.loops(loopnum).isbuffered
        %arm, trigger, wait, save it all
    else 
        %arm, trigger, watch & save
    end
else
    %% iterate through points
    for i=1:thisloop.npoints
        %transform?
        %set channel values
        amset([amscan.loops(loopnum).setchans],points(:,i),amscan.loops(loopnum).valrates);
        
        indices=[outerindices {i}];
        
        if loopnum>1
            innerdata=amrunloop(loopnum-1,indices);
            thisloop.data2D(:,i,:)=innerdata;
        end %recursion
        
        for j=1:length(thisloop.setchans) %record setdata in the loop
            thisloop.data(j,i)=points(j,i);   %%%CAN THIS SOMEHOW BE VECTORIZED? look at setfield
            amscan.data.(thisloop.setchans(j).name)(indices{:})=points(j,i);
        end

        for j=1:length(thisloop.getchans) %get data and record it in the loop
            thisloop.data(j+length(thisloop.setchans),i)=amget(thisloop.getchans(j));
            %thisloop.data.(thisloop.getchans(j).name)(j)=amget(thisloop.getchans(j));
            amscan.data.(thisloop.getchans(j).name)(indices{:})=thisloop.data(j+length(thisloop.getchans),i);% thisloop.data.(thisloop.getchans(j).name)(j);
        end
        %amscan.loops(loopnum).getdata(:,i)=amget([amscan.loops(loopnum).getchans]);
        %% Update plots
        for j=1:length(thisloop.plots)
            switch thisloop.plots(j).plottype
                case 'XY'
                    set(thisloop.plots(j).objhands,'XData',thisloop.data(find(thisloop.plots(j).xchan==thesechans),:));
                    if length(thisloop.plots(j).ychan)==1
                        set(thisloop.plots(j).objhands,'YData',thisloop.data(find(thisloop.plots(j).ychan==thesechans),:));
                    elseif length(thisloop.plots(j).ychan)==2
                        set(thisloop.plots(j).objhands(1),'YData',thisloop.data(find(thisloop.plots(j).ychan(1)==thesechans),:));
                        set(thisloop.plots(j).objhands(2),'YData',thisloop.data(find(thisloop.plots(j).ychan(2)==thesechans),:));
                    end
                case 'XYZ'
                    set(thisloop.plots(j).objhands,'XData',thisloop.data2D(find(thisloop.plots(j).xchan==amscan.loops(loopnum-1).allchans),:,:));
                    set(thisloop.plots(j).objhands,'YData',repmat(thisloop.data(find(thisloop.plots(j).ychan==thesechans),:)',1,length(amscan.loops(loopnum-1).npoints),1));
                    set(thisloop.plots(j).objhands,'ZData',thisloop.data2D(find(thisloop.plots(j).zchan==amscan.loops(loopnum-1).allchans),:,:));
            end
            
        end
        drawnow;
        %SAVE

    end
end
data=thisloop.data;
end


